const SpiderBaseClass = require('./SpiderBaseClass');
const _ = require('lodash');

const listSize = 60;//列表页面有多少个主播信息
const referer = 'https://www.huomao.com/channel/all';

class SpiderHuoMao extends SpiderBaseClass {

  static firstListPage() {
    return 1;
  }

  /**
   * 获取其它页码的列表页面
   * @param page
   */
  static async getListPage(page) {
    const res = await SpiderHuoMao.get(SpiderHuoMao.listPageUrl(page),
      {dataType: 'json', headers: SpiderHuoMao.headers(referer)});
    const hasNextPage = res.data.data.channelList.length > 0;
    const list = res.data.data.channelList.filter(room => room.is_live).
      map(room => {
        return {
          anchor: {
            room: room.room_number,
            name: room.nickname,
            gender: 0,
            avatar: room.headimg.normal,
          },
          type: room.gameCname,
          data: {
            num_watch: SpiderHuoMao.toNumber(room.originviews),
            num_live: 0,
            num_follower: 0,
            num_gift: 0,
          },
        };
      });
    return {
      list,
      page,
      getMoreInfo: true,
      nextPage: page + 1,
      hasNextPage,
    };
  }

  /**
   * 获取直播间信息
   * @param room
   */
  static async getRoom(room) {
    const [res, follower] = await Promise.all([
      SpiderHuoMao.get(
        'https://www.huomao.com/mobile/mob_live/' + room.anchor.room,
        {dataType: 'text', headers: SpiderHuoMao.headers(referer)}),
      SpiderHuoMao.get(
        'https://www.huomao.com/' + room.anchor.room,
        {
          dataType: 'text',
          headers: SpiderHuoMao.headers('https//www.huomao.com'),
        }),
    ]);

    //gift
    {
      const match = res.data.match(/up-experience">(\d+)/);
      if (match && match[1])
        room.data.num_gift = SpiderHuoMao.toNumber(match[1]);
    }

    //follower
    {
      const match = follower.data.match(/audienceNumber":(\d+)/);
      if (match && match[1])
        room.data.num_follower = SpiderHuoMao.toNumber(match[1]);
    }
  }

  /**
   * 生成列表页面的网址
   * @param page
   * @return string
   */
  static listPageUrl(page) {
    return 'https://www.huomao.com/channels/channelnew.json?page=' + page +
      '&game_url_rule=all';
  }

  /**
   * 生成直播间信息的网址
   * @param id
   * @return string
   */
  static roomUrl(id) {
  }

}

module.exports = SpiderHuoMao;